<template>
	<view>	
		<view class="login-bg">
			<view class="login-card">
				<view class="uni-btn-v" style="text-align: center;margin-top:30px;margin-bottom:30px;">
					<u-icon name="scan" :size="180" @click="scan" color="#4790EF"></u-icon>
				</view>
				<view class="login-input login-margin-b">
					<!--设备序列号：<input type="text"  @input="onInput" v-model="dessid" placeholder="扫码识别序列号" />-->
					 资产编号：{{dessid}}
				</view>
				<view class="login-input">
					维护人：<input type="text"  v-model="deshow" placeholder="维护人" />
				</view>
				<view class="login-input">
					故障现象：<input type="text"  v-model="deshow" placeholder="故障描述" />
				</view>
				<view class="login-input">
					维护时间：<input type="text"  v-model="deshow" placeholder="维护时间" />
				</view>
				<view class="login-input">
					备注：<input type="text"  v-model="deshow" placeholder="备注" />
				</view>
				<view class="cu-form-group">
					<view class="title">上传图片：<text class="text-red"></text></view>
					<image :src="avatar ? avatar : '../../static/upimg.png'" @tap="uploadimg"></image>
				</view>								
			</view>		
		<view class="login-btn">
			<u-button class="landing" :loading="loading" type="primary" @click="repair">{{ loading ? "提交中...":"提 交"}}</u-button>
		</view>
		</view>
	</view>
</template>
<script>
 
	import permision from "@/common/permission.js"
	export default {
		data() {
			return {		
				title: 'scanCode',
				result: '',
				loading: false,
				dessid: "",
				deshow: "",
				user:"",
				avatar:""				
			}
		},	
		onLoad() {			 
		},		
		methods: {		
			
			async scan() {
				// #ifdef APP-PLUS
				let status = await this.checkPermission();
				if (status !== 1) {
				    return;
				}
				// #endif
				uni.scanCode({
					success: (res) => {
						console.log(res.result)
						this.dessid = res.result
					},
					fail: (err) => {
						// #ifdef MP
						uni.getSetting({
							success: (res) => {
								let authStatus = res.authSetting['scope.camera'];
								if (!authStatus) {
									uni.showModal({
										title: '授权失败',
										content: 'Hello uni-app需要使用您的相机，请在设置界面打开相关权限',
										success: (res) => {
											if (res.confirm) {
												uni.openSetting()
											}
										}
									})
								}
							}
						})
						// #endif
					}
				});
			},			// #ifdef APP-PLUS
			async checkPermission(code) {
				let status = permision.isIOS ? await permision.requestIOS('camera') :
					await permision.requestAndroid('android.permission.CAMERA');

				if (status === null || status === 1) {
					status = 1;
				} else {
					uni.showModal({
						content: "需要相机权限",
						confirmText: "设置",
						success: function(res) {
							if (res.confirm) {
								permision.gotoAppSetting();
							}
						}
					})
				}
				return status;
			},// #endif
			uploadimg() {
				var that = this
				//上传头像
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera', 'album'], //从相册选择
					success: function(res) {
						api.uploadFile({
							url: 'device/Index/one',
							filePath: res.tempFilePaths[0], //这个是微信临时地址
							name: 'file',
							success: (uploadFileRes) => {
								//console.log(uploadFileRes)
								if (uploadFileRes.code == 0) {
									uni.showToast({
										title: uploadFileRes.msg,
										mask: false,
										duration: 1500
									});
								} else {
									uni.showToast({
										title: '图片上传成功',
										icon: "success",
										duration: 2000
									})
									that.avatar = uploadFileRes.data.url; //这个是服务器返回的存储地址，不是微信临时地址									
								}
							}
						})
					}
				})
			},
			async repair(){
				var user;
				user = uni.getStorageSync('user');				
				var user_nickname;				
				user_nickname = user.user_nickname;
				var branch = user.branch;
				var mobile = user.mobile;
				console.log(mobile);					
				this.loading = true;				
				if (this.dessid == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入序列号（扫码自动显示）'
					});
					this.loading = false;
					return;
				}
				if (this.deshow == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入故障现象'
					});
					this.loading = false;
					return;
				}				
				api.post({
					url: 'device/Index/repaire',
					data: {
						dessid: this.dessid,
						deshow: this.deshow,
						avatar: this.avatar,
						device_type: api.DeviceType,
						branch:branch,
						user_nickname:user_nickname,
						mobile:mobile,
						admin_email :api.admin_email
					},
					success: data => {
						console.log(data);
						if (data.code == 1) {
							this.loading = false;
							//console.log(data);
							uni.showToast({
								duration: 500,
								title: data.msg
							});
							//强制页面重载，跳转到首页
							uni.reLaunch({
								url: '../index/index'
							});							
							setTimeout((e => {
								uni.navigateBack();
							}), 500);
						}
						if (data.code == 0) {
							this.loading = false;
							uni.showToast({
								duration: 1500,
								icon: 'none',
								title: data.msg
							});
						}
					}
				})
			},
		}
	}
</script>

<style>

	.scan-result {
		min-height: 50upx;
		line-height: 50upx;
	}
	.logbt {
		background: none;
		border: none !important;
		position: fixed;
		display: inline;
		margin-left: -76upx
	}

	.logbt:after {
		border: none !important;
	}

	image {
		width: 100upx;
		height: 100upx;
	}

	.landing {
		height: 84upx;
		line-height: 84upx;
		border-radius: 44upx;
		font-size: 32upx;
		background: linear-gradient(left, #86B5F4, #4790EF);
	}

	.login-btn {
		padding: 10upx 20upx;
		margin-top: 10upx;
	}

	.login-function {
		overflow: auto;
		padding: 20upx 20upx 30upx 20upx;
	}

	.login-forget {
		float: left;
		font-size: 26upx;
		color: #999;
	}

	.login-register {
		color: #666;
		float: right;
		font-size: 26upx;

	}

	.login-input input {
		background: #F2F5F6;
		font-size: 28upx;
		padding: 10upx 25upx;
		height: 62upx;
		line-height: 62upx;
		border-radius: 8upx;
	}

	.login-margin-b {
		margin-bottom: 25upx;
	}

	.login-input {
		padding: 10upx 20upx;
	}

	.login-head {
		font-size: 34upx;
		text-align: center;
		padding: 25upx 10upx 55upx 10upx;
	}

	.login-card {
		background: #fff;
		border-radius: 12upx;
		padding: 10upx 25upx;
		box-shadow: 0 6upx 18upx rgba(0, 0, 0, 0.12);
		position: relative;
		margin-top: 120upx;
	}

	.login-bg {
		height: 260upx;
		padding: 25upx;
		background: linear-gradient(#86B5F4, #4790EF);
	}
	.upload_img_item{
	position: relative;
}

.upload_img_close{
	position: absolute;
	z-index: 999;
	right: 0;
	color:#fff;
	background: #DD524D;
}
.upload_img{
	width: 90px;
	height: 90px;
	display: block;
}
</style>
